package com.yzkj.cpjxc.business.service.impl;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.framework.utils.DateTimeUtils;
import com.yzkj.cpjxc.business.domain.JieZhang;
import com.yzkj.cpjxc.business.domain.JieZhangQueryVo;
import com.yzkj.cpjxc.business.service.JieZhangService;
import com.yzkj.cpjxc.business.domain.PingZhengQueryVo;
import com.yzkj.cpjxc.business.service.PingZhengService;
import com.yzkj.cpjxc.business.domain.ZhangTao;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 结账 Service 实现
 */
@Service
public class JieZhangServiceImpl implements JieZhangService {

	/**
	 * MyBatis 命名空间
	 */
	private final static String MAPPER_NAMESPACE = "com.chkj.xezz.jieZhang.mappers.JieZhangMapper";

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Autowired
	private PingZhengService pingZhengService;
	
	@Override 
	public BusinessMap create(JieZhang jieZhang) throws Exception {
		BusinessMap bm = new BusinessMap();
		if (this.isExists(jieZhang)) {
			bm.put(Constants.WARNING_MSG, "创建下一期结账信息已经存在，请联系系统管理员！");
			return bm;
		}
		sqlSessionTemplate.insert(MAPPER_NAMESPACE + ".add", jieZhang);
		return bm;
	}
	
	@Override 
	public BusinessMap createJieZhang(JieZhang jieZhang, JieZhang jz, ZhangTao zt) throws Exception {
		BusinessMap bm = new BusinessMap();
		//新增下一个未结账账期
		bm = this.create(jieZhang);
		if(bm.getIsSucc()) {
			//将当前账期修改为已结账状态
			JieZhang temp = new JieZhang();
			temp.setIsYjz(Constants.IS_YJZ);
			temp.setUuid(jz.getUuid());
			this.edit(temp);

			//将当前账期所有凭证修改为已结账
			PingZhengQueryVo queryVo = new PingZhengQueryVo();
			queryVo.setCompanyId(zt.getUuid());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String startTime = jz.getJzYear() + "-" + StringUtils.leftPad(jz.getJzMonth() + "", 2, "0") + "-01";

			Map<String, Date> datemap = DateTimeUtils.getMonthEndDateForStartDate(sdf.parse(startTime));
			queryVo.setStartTime(datemap.get(Constants.START_TIME));
			queryVo.setEndTime(datemap.get(Constants.END_TIME));

			pingZhengService.setPzYjz(queryVo);
		}
		
		return bm;
	}

	@Override
	public boolean isExists(JieZhang jieZhang) throws Exception {
		return (Integer)sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".isExists", jieZhang) > 0;
	}
	
	@Override
	public JieZhang getById(String uuid) throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getById", uuid);
	}
	
	@Override
	public BusinessMap edit(JieZhang jieZhang) throws Exception {
		BusinessMap bm = new BusinessMap();
		//if (this.isExists(jieZhang)) {
		//	bm.put(Constants.WARNING_MSG, "该结账名称已经存在");
		//	return bm;
		//}
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".edit", jieZhang);
		return bm;
	}

	@Override
	public boolean delete(String uuid) throws Exception {
		sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".deleteById", uuid);
		return true;
	}

	@Override
	public Pager<JieZhang> search(Pager<JieZhang> pager, JieZhangQueryVo queryVo) throws Exception {
		List<JieZhang> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".page", queryVo, new RowBounds((pager.getCurrentPage() - 1) * pager.getLimit(), pager.getLimit()));
		pager.setDataList(dataList);
		
		// 如果需要转换成ShowVo展示，那么就在这里转换
		
		Long total = sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTotal", queryVo);
		pager.setTotal(total);
		return pager;
	}

	@Override
	public JieZhang getByZhangTaoId(String zhangTaoId) throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getByZhangTaoId", zhangTaoId);
	}

}
